package com.ruoyi.project.beautyflower.order.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.app.request.OrderRepayRequest;
import com.ruoyi.project.app.response.OrderRepayPlanResponse;
import com.ruoyi.project.app.util.KxtSmsUtil;
import com.ruoyi.project.beautyflower.member.domain.Member;
import com.ruoyi.project.beautyflower.member.mapper.MemberMapper;
import com.ruoyi.project.tool.push.JPushUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.beautyflower.order.mapper.OrderMapper;
import com.ruoyi.project.beautyflower.order.domain.Order;
import com.ruoyi.common.support.Convert;

/**
 * 订单  服务层实现
 *
 * @author lxs
 * @date 2018-10-15
 */
@Service
@SuppressWarnings("ALL")
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    /**
     * 查询订单 信息
     *
     * @param id 订单 ID
     * @return 订单 信息
     */
    @Override
    public Order selectOrderById(Integer id) {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单 列表
     *
     * @param order 订单 信息
     * @return 订单 集合
     */
    @Override
    public List<Order> selectOrderList(Order order) {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单
     *
     * @param order 订单 信息
     * @return 结果
     */
    @Override
    public int insertOrder(Order order) {
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单
     *
     * @param order 订单 信息
     * @return 结果
     */
    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    /**
     * 删除订单 对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(String ids) {
        return orderMapper.deleteOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 查询还款列表
     *
     * @param request 请求参数
     * @return 结果
     */
    @Override
    public List<OrderRepayPlanResponse> repayPlanList(OrderRepayRequest request) {
        return orderMapper.repayPlanList(request);
    }

    /**
     * 根据订单号查询订单
     *
     * @param orderNumber
     * @return
     */
    @Override
    public Order selectOrderByListOrderNumber(String orderNumber) {
        return orderMapper.selectOrderByListOrderNumber(orderNumber);
    }

    /**
     * 根据用户查询借款计划
     *
     * @param order
     * @return
     */
    @Override
    public List<Order> selectOrderListByuserId(Order order) {
        return orderMapper.selectOrderListByuserId(order);
    }

    @Override
    public String getLoanTotal(String memberId) {
        return orderMapper.getLoanTotal(memberId);
    }

    @Override
    public Object sendNotice(Integer orderId, String memberId, String msg,String sms) {
        Member member=memberMapper.selectMemberById(Integer.valueOf(memberId));
        if (StringUtils.isNotBlank(memberId)) {
            if (orderId != null) {
                String title = msg;
                Map<String, String> extra = new HashMap<>();
                extra.put("type", "1");
                // 订单ID
                extra.put("id", String.valueOf(orderId));
                JPushUtil.get().pushAlias(member.getUuid()+"+"+memberId, title, extra);

            }
        }
        if (StringUtils.isNotBlank(sms)) {
            if(sms.equals("on")){
                JSONObject jsonOs =KxtSmsUtil.sendSms(member.getMobile(),msg);
                System.out.println(jsonOs);
            }
        }
        return null;
    }

}
